/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Ventas;

import Atxy2k.CustomTextField.RestrictedTextField;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import Controlador.Bean.Ventas.Cliente;
import Controlador.Bean.Ventas.ClienteBusqueda;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Luis Enrique
 */
public class ClienteMainPanel extends javax.swing.JPanel {

    ClienteTableModel myTableModel;
    ArrayList<Cliente> clientes = Controlador.Service.Ventas.GestionVentas.getInstance().queryAllClientes();
    
    class ClienteTableModel extends AbstractTableModel{

        String [] titles = {"Código", "Tipo de Persona","Razón Social/Apellidos y Nombres","Núm. de Documento", "Estado"};

        @Override
        public int getRowCount() {
            return clientes.size();
        }

        @Override
        public int getColumnCount() {
            return titles.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            String res=null;
            Cliente c = clientes.get(rowIndex);
            switch(columnIndex){
                case 0: res = c.getIdCliente();break;
                case 1: res = (c.getTipo().equals("TPJ"))?"Jurídica":"Natural";break;
                case 2: res = (c.getRazonSocial()!=null)?c.getRazonSocial():(c.getApePaterno()+" "+c.getApeMaterno()+", "+c.getNombres());break;
                case 3: res = c.getNumDocumento();break;
                case 4: res = c.getEstado();break;
            }
            return res;
        }
        
        @Override
        public String getColumnName(int col){
            return titles[col];
        }
    }
    
    public ClienteMainPanel() {
        initComponents();
        btnGpTipoPersona.add(rbPJuridica);
        btnGpTipoPersona.add(rbPNatural);
        btnGpTipoDocumento.add(rbRUC);
        btnGpTipoDocumento.add(rbDNI);
        
        RestrictedTextField restricted = new RestrictedTextField(txtRazonSocial);
        restricted.setLimit(50);
        
        restricted = new RestrictedTextField(txtApePaterno);
        restricted.setLimit(50);
        restricted.setOnlyText(true);
                
        restricted = new RestrictedTextField(txtApeMaterno);
        restricted.setLimit(50);
        restricted.setOnlyText(true);
        
        restricted = new RestrictedTextField(txtNombres);
        restricted.setLimit(50);
        restricted.setOnlyText(true);
                
        restricted = new RestrictedTextField(txtNumDoc);
        restricted.setLimit(11);
        restricted.setOnlyNums(true);
        
        myTableModel = new ClienteTableModel();
        tblClientes.setModel(myTableModel);
        TableRowSorter<ClienteTableModel> sorter = new TableRowSorter<ClienteTableModel> ((ClienteTableModel) tblClientes.getModel());
        tblClientes.setRowSorter(sorter);
    }

    public void refreshItemsTable(){
        clientes = Controlador.Service.Ventas.GestionVentas.getInstance().queryAllClientes();
        myTableModel.fireTableChanged(null);        
    }
    
    public void refreshItemsTable2(ArrayList<Cliente> clientes){
        this.clientes = new ArrayList(clientes);
        myTableModel.fireTableChanged(null);        
    }
    
    public void refreshDetails(){
        if(rbPJuridica.isSelected()){
            txtRazonSocial.setText("");
            txtRazonSocial.setEnabled(true);
            txtApePaterno.setText("");
            txtApePaterno.setEnabled(false);
            txtApeMaterno.setText("");
            txtApeMaterno.setEnabled(false);
            txtNombres.setText("");
            txtNombres.setEnabled(false);
            rbRUC.setSelected(true);
            rbRUC.setEnabled(false);
            rbDNI.setEnabled(false);
            txtNumDoc.setText("");
            txtNumDoc.setEnabled(true);
        }
        else{
            txtRazonSocial.setText("");
            txtRazonSocial.setEnabled(false);
            txtApePaterno.setText("");
            txtApePaterno.setEnabled(true);
            txtApeMaterno.setText("");
            txtApeMaterno.setEnabled(true);
            txtNombres.setText("");
            txtNombres.setEnabled(true);
            rbDNI.setSelected(true);
            btnGpTipoDocumento.clearSelection();
            rbRUC.setEnabled(true);
            rbDNI.setEnabled(true);            
            txtNumDoc.setText("");
            txtNumDoc.setEnabled(true);
        }    
    }
    
    public void limpiarCriterios(){
        btnGpTipoPersona.clearSelection();
        txtRazonSocial.setText("");
        txtRazonSocial.setEnabled(true);
        txtApePaterno.setText("");
        txtApePaterno.setEnabled(true);
        txtApeMaterno.setText("");
        txtApeMaterno.setEnabled(true);
        txtNombres.setText("");
        txtNombres.setEnabled(true);
        btnGpTipoDocumento.clearSelection();
        rbRUC.setEnabled(true);
        rbDNI.setEnabled(true);
        txtNumDoc.setText("");
        txtNumDoc.setEnabled(true);
        cmbEstado.setSelectedIndex(0);
    }
    
    public boolean valido(){
        boolean b = true;
        
        if(rbRUC.isSelected())
            if(!txtNumDoc.getText().isEmpty())
                if(txtNumDoc.getText().length()!=11){
                    b = false;
                    JOptionPane.showMessageDialog(this, "Error en Num. de Documento RUC", "Error en los datos", JOptionPane.OK_OPTION);
                }           
        if(rbDNI.isSelected())
            if(!txtNumDoc.getText().isEmpty())
                if(txtNumDoc.getText().length()!=11 || txtNumDoc.getText().length()!=8){
                    b = false;
                    JOptionPane.showMessageDialog(this, "Error en Num. de Documento DNI", "Error en los datos", JOptionPane.OK_OPTION);
                }
        return b;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGpTipoPersona = new javax.swing.ButtonGroup();
        btnGpTipoDocumento = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblClientes = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        rbPJuridica = new javax.swing.JRadioButton();
        rbPNatural = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        txtRazonSocial = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtApePaterno = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtApeMaterno = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtNombres = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        rbRUC = new javax.swing.JRadioButton();
        rbDNI = new javax.swing.JRadioButton();
        jLabel7 = new javax.swing.JLabel();
        txtNumDoc = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        cmbEstado = new javax.swing.JComboBox();
        btnBuscar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        jLabel16 = new javax.swing.JLabel();
        btnAgregar = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(784, 549));
        setMinimumSize(new java.awt.Dimension(784, 549));
        setPreferredSize(new java.awt.Dimension(784, 549));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Clientes"));
        jPanel1.setToolTipText("");
        jPanel1.setMaximumSize(new java.awt.Dimension(764, 305));
        jPanel1.setMinimumSize(new java.awt.Dimension(764, 305));
        jPanel1.setPreferredSize(new java.awt.Dimension(764, 305));
        jPanel1.setLayout(new java.awt.BorderLayout());

        tblClientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblClientes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblClientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblClientesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblClientes);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterios de Búsqueda"));

        jLabel1.setText("Tipo de Persona");

        rbPJuridica.setText("P. Jurídica");
        rbPJuridica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbPJuridicaActionPerformed(evt);
            }
        });

        rbPNatural.setText("P. Natural");
        rbPNatural.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbPNaturalActionPerformed(evt);
            }
        });

        jLabel2.setText("Razón Social");

        jLabel3.setText("Ape. Paterno");

        jLabel4.setText("Ape. Materno");

        jLabel5.setText("Nombres");

        jLabel6.setText("Tipo de Doc.");

        rbRUC.setText("R.U.C.");

        rbDNI.setText("D.N.I.");

        jLabel7.setText("Número de Doc.");

        jLabel8.setText("Estado");

        cmbEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar", "Activo", "Inactivo" }));

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel16.setText("R.U.C. (11 dígitos) y D.N.I. (8 dígitos)");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addGap(40, 40, 40)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtApeMaterno)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(rbPJuridica)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                        .addComponent(rbPNatural))
                    .addComponent(txtApePaterno)
                    .addComponent(txtRazonSocial)
                    .addComponent(txtNombres, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(btnBuscar)
                                .addGap(18, 18, 18)
                                .addComponent(btnLimpiar))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(rbRUC)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE)
                                .addComponent(rbDNI))
                            .addComponent(txtNumDoc, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cmbEstado, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel16)))
                .addGap(28, 28, 28))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(rbPNatural)
                    .addComponent(rbPJuridica)
                    .addComponent(jLabel6)
                    .addComponent(rbDNI)
                    .addComponent(rbRUC))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtRazonSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNumDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnLimpiar)
                            .addComponent(btnBuscar)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtApePaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtApeMaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnAgregar.setText("Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnAgregar)
                .addGap(18, 18, 18)
                .addComponent(btnModificar)
                .addGap(18, 18, 18)
                .addComponent(btnEliminar)
                .addGap(10, 10, 10))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnModificar)
                    .addComponent(btnAgregar)
                    .addComponent(btnEliminar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(61, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void rbPNaturalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbPNaturalActionPerformed
        refreshDetails();
    }//GEN-LAST:event_rbPNaturalActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        ClienteAgregarJDialog agregarDialog = new ClienteAgregarJDialog(null, true);
        agregarDialog.setRefClientePanel(this);
        agregarDialog.setVisible(true);
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        if(tblClientes.getSelectedRow()!=-1){
            int rowSel = tblClientes.getSelectedRow();
            ClienteModificarJDialog modificarDialog = new ClienteModificarJDialog(null, true);
            modificarDialog.setRefClientePanel(this);
            modificarDialog.setCliente(clientes.get(rowSel));
            modificarDialog.setVisible(true);            
            refreshItemsTable();
        }
        else
            JOptionPane.showMessageDialog(this,"Debe seleccionar un registro de la tabla.");
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        if(tblClientes.getSelectedRow()!=-1){
            int rowSel = tblClientes.getSelectedRow();
            int confirma = JOptionPane.showConfirmDialog(this,"¿Desea eliminar el cliente: "+clientes.get(rowSel).getIdCliente()+"?","Confirmar",2);
            if (JOptionPane.OK_OPTION==confirma){    
                boolean flag;
                flag = Controlador.Service.Ventas.GestionVentas.getInstance().eliminarCliente(clientes.get(rowSel).getIdCliente());
                if(flag){
                    JOptionPane.showMessageDialog(this,"Se eliminó correctamente el cliente.");
                    refreshItemsTable();
                }
                else
                    JOptionPane.showMessageDialog(this,"No se puede eliminar el registro, ya que existen documentos asociados.");
            }
        }
        else
            JOptionPane.showMessageDialog(this,"Debe seleccionar un registro de la tabla.");
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void rbPJuridicaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbPJuridicaActionPerformed
        refreshDetails();
    }//GEN-LAST:event_rbPJuridicaActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        limpiarCriterios();
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void tblClientesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblClientesMouseClicked
        if(evt.getClickCount()==2){
            int rowSel = tblClientes.getSelectedRow();
            ClienteVerJDialog verDialog = new ClienteVerJDialog(null, true);
            verDialog.setCliente(clientes.get(rowSel));
            verDialog.setVisible(true);            
        }
    }//GEN-LAST:event_tblClientesMouseClicked

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        if(valido()){
            String tipo;
            if(rbPJuridica.isSelected())
                tipo = "TPJ";
            else if (rbPNatural.isSelected())
                tipo = "TPN";
            else
                tipo=null;
            String razonSocial = (txtRazonSocial.getText().equals(""))?null:"%"+txtRazonSocial.getText()+"%";
            String apePaterno = (txtApePaterno.getText().equals(""))?null:"%"+txtApePaterno.getText()+"%";
            String apeMaterno = (txtApeMaterno.getText().equals(""))?null:"%"+txtApeMaterno.getText()+"%";
            String nombres = (txtNombres.getText().equals(""))?null:"%"+txtNombres.getText()+"%";
            String tipoDoc;
            if(rbDNI.isSelected())
                tipoDoc = "dni";
            else if (rbRUC.isSelected())
                tipoDoc = "ruc";
            else
                tipoDoc=null;
            String numDoc = (txtNumDoc.getText().equals(""))?null:txtNumDoc.getText();
            String estado;
            if(cmbEstado.getSelectedIndex()==1)
                estado = "Activo";
            else if (cmbEstado.getSelectedIndex()==2)
                estado = "Inactivo";
            else
                estado=null;

            ClienteBusqueda clienteBusqueda = new ClienteBusqueda(tipo,razonSocial,apePaterno,apeMaterno,nombres,tipoDoc,numDoc,estado);
            ArrayList<Cliente> clientesConsulta = Controlador.Service.Ventas.GestionVentas.getInstance().queryClientes(clienteBusqueda);
            refreshItemsTable2(clientesConsulta);
        }
    }//GEN-LAST:event_btnBuscarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.ButtonGroup btnGpTipoDocumento;
    private javax.swing.ButtonGroup btnGpTipoPersona;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JComboBox cmbEstado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JRadioButton rbDNI;
    private javax.swing.JRadioButton rbPJuridica;
    private javax.swing.JRadioButton rbPNatural;
    private javax.swing.JRadioButton rbRUC;
    private javax.swing.JTable tblClientes;
    private javax.swing.JTextField txtApeMaterno;
    private javax.swing.JTextField txtApePaterno;
    private javax.swing.JTextField txtNombres;
    private javax.swing.JTextField txtNumDoc;
    private javax.swing.JTextField txtRazonSocial;
    // End of variables declaration//GEN-END:variables
}
